import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/datas/game_subcription_item.dart';
import 'package:flutter_syt/datas/provider_background.dart';
import 'package:flutter_syt/datas/provider_index_setting.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/domain/mine/service/provider_unread_msg.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/global_common.dart';
import 'package:flutter_syt/routes.all.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/widgets/refresher_configuration.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

import 'domain/main_page.dart';
import 'domain/splash/splash.dart';
import 'theme/button_styles.dart';
import 'theme/theme_color.dart';

class CustomSystemUiOverlayStyle {
  static const SystemUiOverlayStyle light = SystemUiOverlayStyle(
    systemNavigationBarColor: Color(0xFFFFFFFF),
    systemNavigationBarDividerColor: null,
    // statusBarColor: null,
    statusBarColor: Colors.transparent,
    systemNavigationBarIconBrightness: Brightness.light,
    statusBarIconBrightness: Brightness.light,
    statusBarBrightness: Brightness.dark,
  );

  static const SystemUiOverlayStyle dark = SystemUiOverlayStyle(
    systemNavigationBarColor: Color(0xFFFFFFFF),
    systemNavigationBarDividerColor: null,
    // statusBarColor: null,
    statusBarColor: Colors.transparent,
    systemNavigationBarIconBrightness: Brightness.light,
    statusBarIconBrightness: Brightness.dark,
    statusBarBrightness: Brightness.light,
  );
}

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

class AppRootWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // SystemChrome.setEnabledSystemUIOverlays(
    //     [SystemUiOverlay.top, SystemUiOverlay.bottom]);
    // SystemChrome.setSystemUIOverlayStyle(style)
    // SystemChrome.setSystemUIOverlayStyle(CustomSystemUiOverlayStyle.dark);
    // SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    var behavior = ScrollBehavior()..copyWith(physics: ClampingScrollPhysics());
    Locale? local;

    var storyLocal = appContext.storage.getString("storyLocal") ;
    if (storyLocal != null) {
      local = Locale(storyLocal);
    }
    var content = MaterialApp(
      navigatorKey: navigatorKey,
      scrollBehavior: behavior,
      localizationsDelegates: const [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      // showSemanticsDebugger: false,
      debugShowCheckedModeBanner: false,
      supportedLocales: S.delegate.supportedLocales,
      locale: local,
      title: storyLocal?.contains('cn') == true ?  'Chengdu 2025' : '世运通' ,
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.white,
        appBarTheme: const AppBarTheme(
          backgroundColor: ThemeColor.bkColorFFFFFF,
          foregroundColor: ThemeColor.colorBlack100,
          centerTitle: true,
          elevation: 0.5,
          titleTextStyle: FontStyles.headerTextStyle_18,
        ),
        // Define the default brightness and colors.
        brightness: Brightness.light,
        primaryColor: ThemeColor.primaryColor,

        elevatedButtonTheme:
            ElevatedButtonThemeData(style: MyButtonStyles.elevatedButtonStyle),
        outlinedButtonTheme:
            OutlinedButtonThemeData(style: MyButtonStyles.outlinedButtonStyle),

        // Define the default `TextTheme`. Use this to specify the default
        textTheme: const TextTheme(
          // headline1: TextStyle(fontSize: 46.0, fontWeight: FontWeight.bold),
          // headline6: TextStyle(fontSize: 16.0, fontStyle: FontStyle.italic),
          // bodyText1: TextStyle(
          //     fontSize: 16.0,
          //     fontStyle: FontStyle.normal,
          //     color: Color(0xFF000000)),
          // button: TextStyle(
          //     fontSize: 16.0,
          //     fontStyle: FontStyle.normal,
          //     color: Color(0xFF000000)),
        ),
      ),
      // home: LoginByPasswordPage(),
      // home: LoginByCodePage(),
      builder: EasyLoading.init(
        builder: (context, child) {
          // ScreenUtil.setContext(context);
          ScreenUtil.init(
            context,
            designSize: Size(375, 812),
            minTextAdapt: true,
            splitScreenMode: true,
          );
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: child ?? Container(),
          );
        },
      ),
      home: Splash(
        getMainView: getMainView,
      ),
      routes: routes(),
      navigatorObservers: [appContext.rootObserver],
    );
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (ctx) => UserInfo.me,
        ),
        ChangeNotifierProvider(
          create: (ctx) => GameSubcriptionManageData.model,
        ),
        ChangeNotifierProvider(create: (ctx) => GlobalData.data),
        // ChangeNotifierProvider(create: (ctx) => OnlineGlobalData.shared),
        ChangeNotifierProvider(create: (ctx) => ProviderIndexSetting()),
        ChangeNotifierProvider(create: (ctx) => ProviderUnReadMsg()),
        ChangeNotifierProvider(create: (ctx) => ProviderBackground()),
      ],
      child: DyRefreshConfiguration(content),
    );
    // return ScreenUtilInit(
    //     designSize: Size(375, 812),
    //     minTextAdapt: true,
    //     splitScreenMode: true,
    //     builder: () => );
  }

  Future getMainView() async {
    return MainPage();
  }
}
